<?php
	class BaseController
	{
		private $dbName;
		
		private $connection;
	
		public function BaseController( $dbName )
		{
			$this->dbName = $dbName;
			
			$this->connection = NULL;
			$this->connect();
		}
		
		protected function connect()
		{
			$this->connection = mysqli_connect( "localhost", "user", "service", $this->dbName );

			if ( mysqli_connect_errno( $this->connection ) )
			{
				echo "Failed to connect to MySQL: " . mysqli_connect_error();
				return false;
			}	
			
			return true;
		}
		
		public function runQuery( $query )
		{
			//establish a connection if not connected
			if ( $this->connection == NULL )
				if ( !$this->connect() )
					return NULL;
	
			return mysqli_query( $this->connection, $query );
		}
		
		public function getConnection()
		{
			return $this->connection;
		}
		
		public function close()
		{
			mysqli_close( $this->connection );
		}
	}
?>